﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using SqlSugar;

namespace FineUIMvc
{
    public static partial class FineUIMvcExtensions
    {
        /// <summary>
        /// 绑定BindGrid
        /// </summary>
        /// <param name="grid">grid控件帮助类</param>
        /// <param name="fc">表单</param>
        /// <param name="pageSizeID">分页size控件id</param>
        /// <param name="queryable">查询表达式</param>
        /// <returns></returns>
        public static void BindGrid<T>(this GridAjaxHelper grid, FormCollection fc, string pageSizeID, ISugarQueryable<T> queryable)
        {
            int recordCount = 0;
            int pageSize = int.Parse(fc[pageSizeID]);

            var tempData= queryable.ToPageList(int.Parse(fc[grid.Source.ID+"_pageIndex"]) + 1, pageSize, ref recordCount);
            BindGrid(grid, fc, pageSizeID, tempData, recordCount);
        }

        /// <summary>
        /// 绑定BindGrid
        /// </summary>
        /// <param name="grid">grid控件帮助类</param>
        /// <param name="fc">表单</param>
        /// <param name="pageSizeID">分页size控件id</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="recordCount">数据总数</param>
        /// <returns></returns>
        public static void BindGrid(this GridAjaxHelper grid, FormCollection fc,string pageSizeID,dynamic dataSource, int recordCount)
        {
            //.OnPageIndexChanged(Url.Action("Grid1_PageIndexChanged"),fields:"Panel1")  fields可以为表单容器对应FormCollection
            //FormCollection 常见参数
            //Grid1_pagingToolbar_pageNumberBox
            //ddlPageSize //自定义
            //Grid1_fields[]
            //Grid1_pageIndex
            //Grid1_pageSize

            //GridAjaxHelper grid1 = UIHelper.Grid("Grid1");
            int pageSize = int.Parse(fc[pageSizeID]);
            grid.DataSource(dataSource, fc[grid.Source.ID + "_fields[]"].Split(','));
            grid.RecordCount(recordCount);
            grid.PageSize(pageSize);
        }
    }
}
